#pragma once 


#include <iostream>
#include <string>
#include <assert.h>
#include <cstdlib>
#include <vector>
#include <unordered_map>
#include <fstream>


#include "../comm/util.hpp"
#include "../comm/log.hpp"

const std::string question_path = "./questions/";
const std::string question_list = "./questions/questions.list";

namespace ns_model
{

    using namespace ns_util;
    using namespace ns_logUtil;

    struct Question
    {
        std::string number;  // 题号
        std::string title; // 题目的标题
        std::string star; // 难度
        int cpu_limit;
        int mem_limit;
        std::string desc; // 题目的描述
        std::string header; // 题目预设给用户的代码
        std::string tail; // 题目的测试用例，需要和header拼接，形成完整代码

    };
    class Model
    {
    private:    
        std::unordered_map<std::string, Question> questions;

    public:
        Model()
        {
            assert(LoadQuestionList(question_list));
        }

        bool LoadQuestionList(const std::string &question_list)
        {
            std::ifstream in(question_list);
            if(!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，请检查是否存在题库文件" << std::endl;
                return false;
            }

            std::string line;
            while(getline(in, line))
            {
                std::vector<std::string> tokens;
                //std::cout << line << std::endl;
                StringUtil::SplitString(line, tokens, " ");
                // //Debug#
                // for(int i = 0; i < tokens.size(); i++)
                // {
                //     std::cout << tokens[i] << " ";
                // }
                // //Debug/

                // 1 回文数 简单 1 5000
                if(tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败，请检查文件格式" << std::endl;
                    continue;
                }

                Question q;
                q.number = tokens[0];
                q.title = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                std::string path = question_path; // "./questions/";
                path += q.number;
                path += "/";
                
                //std::cout <<"lujin:  " << path << "\n";
                // bool ReadFile(const std::string &target, std::string *content, bool keep = false)
                FileUtil::ReadFile(path+"desc.txt", &(q.desc), true); //path = ./questions/q.number/
                FileUtil::ReadFile(path+"header.cpp", &(q.header), true);
                FileUtil::ReadFile(path+"tail.cpp", &(q.tail), true);

                // // debug
                // std::cout << "q.number:  " << q.number << "q.title" << q.title << std::endl;
                // std::cout << "q.desc:  " << q.desc << "q.header" << q.header << std::endl;

                questions.insert({q.number, q});
            }


            LOG(INFO) << "加载题库...成功！" << std::endl;
            in.close();

            return true;
        }



        bool GetAllQuestions(std::vector<Question> *out)
        {
            if(questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << std::endl;
                return false;
            }

            for(const auto &q : questions)
            {
                out->push_back(q.second);
            }
            LOG(INFO)<< "用户获取全部题目成功" <<std::endl;
            return true;
        }

        bool GetOneQuestions(const std::string &number, Question *q)
        {
            const auto &iter = questions.find(number);
            if(iter == questions.end())
            {
                LOG(ERROR) << "用户获取题目失败，获取的题目编号为：" << number << std::endl;
                return false;
            }

            (*q) = iter->second;

            LOG(INFO)<< "用户获取指定题目成功" <<std::endl;
            return true;
        }
        


        ~Model()
        {}
    };
}